package cn.lidar.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FileSortUtils {
    public static List<File> fileSort(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        List<File> list = new ArrayList<>();
        if (files != null) {
            for (File file2 : files) {
                list.add(file2);
            }
        }

        Collections.sort(list, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                Integer f = f(o1.getName());
                Integer f2 = f(o2.getName());
                return Integer.compare(f, f2);
            }
        });
        return list;
    }

    static Integer f(String filename) {
        int x = filename.indexOf(".");
        String string2 = filename.substring(0, x);
        char[] cs = string2.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < cs.length; i++) {
            if (Character.isDigit(cs[i])) {
                builder.append(cs[i]);
            }
        }
        return Integer.parseInt(builder.toString());
    }
}